/**
 * 
 */
package com.dhcc.DRGsHosp.web.controller;

import com.dhcc.DRGsHosp.facade.DictionaryFacade;
import com.dhcc.DRGsHosp.facade.InitializeFacade;
import org.codehaus.jackson.map.ObjectMapper;
import org.openkoala.koala.commons.InvokeResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author chl
 *
 */
@Controller("drgsHospWebController9")
@RequestMapping(value = "/dictionary")
public class DictionaryController {
	@Autowired
	private DictionaryFacade dictionaryFacade ;
	
	@Autowired
	private InitializeFacade initializeFacade ;
	/**
	 * 
	 *  @desp  ：获取字典列表
	 *  @Author： lhj
	 *  @return
	 *  @Date  :2018年2月6日下午8:59:25
	 */
	@ResponseBody
	@RequestMapping(value = "/getDictionaryList")
	public InvokeResult getDictionaryList(){
		return dictionaryFacade.findAllDictionary() ;
	}
	
	/**
	 *  @desp  ：获取字典明细
	 *  @Author： lhj
	 *  @param id
	 *  @return
	 *  @Date  :2018年2月6日下午7:22:42
	 */
	@ResponseBody
	@RequestMapping(value = "/getDictionaryDataByCode")
	public InvokeResult getDictionaryDataByCode(String dicCode){
		return dictionaryFacade.getDictionaryDataByCode(dicCode);
	}
	/**
	 *  @desp  ：获取字典
	 *  @Author： lhj
	 *  @param dicCode
	 *  @return
	 *  @Date  :2018年2月6日下午7:37:26
	 */
	@ResponseBody
	@RequestMapping(value = "/findDictionaryByCode")
	public InvokeResult findDictionaryByCode(String dicCode){
		return dictionaryFacade.findDictionaryByCode(dicCode);
	}
	/**
	 *  @desp  ：删除字典
	 *  @Author： lhj
	 *  @param dicCode
	 *  @return
	 *  @Date  :2018年2月11日下午3:30:34
	 */
	@ResponseBody
	@RequestMapping(value = "/delDictionary")
	public InvokeResult delDictionary(String dicCode) {
		try {
			dictionaryFacade.removeDictionary(dicCode);
			initializeFacade.initializeSysDictionary();
			return InvokeResult.success();
		} catch (Exception e) {
			e.printStackTrace();
			return InvokeResult.failure("删除失败!");
		}
	}
	/**
	 *  @desp  ：保存字典
	 *  @Author： lhj
	 *  @param param
	 *  @return
	 *  @Date  :2018年2月6日下午5:29:27
	 */
	@ResponseBody
	@RequestMapping(value = "/saveDictionary")
	public InvokeResult saveDictionary(String param){
		ObjectMapper objectMapper = new ObjectMapper();
		Map<String, Object> json;
		try {
			json = objectMapper.readValue(param, Map.class);
			dictionaryFacade.saveDictionary(json);
			initializeFacade.initializeSysDictionary();
			return InvokeResult.success();
		} catch (Exception e) {
			e.printStackTrace();
			json = new HashMap<String, Object>();
			return InvokeResult.failure(e.getMessage());
		} 
	}
}
